package controller;
// fixme: 尝试修改，上传git

import controller.result.Code;
import controller.result.Msg;
import controller.result.Result;
import domain.EntityComment;
import domain.ShowComment;
import domain.VirtualComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.CommentService;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("comments")
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/getComments/{fileId}")
    public Result getCommentById(@PathVariable String fileId){

        List<ShowComment> comments =  commentService.getCommentById(Integer.parseInt(fileId));
        //通过传入一个整数类型的 ​fileId​参数，从 ​commentService​对象中获取一个与该 ​fileId​相关的评论列表，将其赋值给 ​comments​变量。

        return new Result(Code.GET_COMMENTS_SUCCESS,comments, Msg.GET_COMMENTS_SUCCESS);
    } //......
    @PostMapping("/addComment")
    public Result addComment(@RequestBody EntityComment entityComment){

        VirtualComment virtualComment = new VirtualComment();
        virtualComment.setUid(entityComment.getUid());
        virtualComment.setFid(entityComment.getFid());
        virtualComment.setContext(entityComment.getContext());
        virtualComment.setComment_date_time(new Timestamp(System.currentTimeMillis()));

        commentService.addComment(virtualComment);
        List<ShowComment> comments =  commentService.getCommentById(entityComment.getFid());
        return new Result(Code.GET_COMMENTS_SUCCESS,comments, Msg.GET_COMMENTS_SUCCESS);
    }
}
